﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace SelectServer
//{
//    class LanSocketBase
//    {
//    }
//}

using System.Diagnostics;
using System.Threading;
//using UnityEngine;

/*
 *轻量级局域网服务器。 
 * 协议如下
 * 消息头前2字节保存当前消息长度
 * 后面跟4字节表示消息ID
 * 再后面是消息实质内容
 */

namespace SelectServer
{
    public class LanSocketBase
    {
        public static int m_MaxOnePackBuff = 1024 * 3;
        public static int m_MaxAllBuff = 1024 * 50;
        public static int m_HeadSize = 6;
        protected static bool m_HasInit = false;
        protected static byte[] m_OnePack;
        protected static int m_OnePackIndex;
        private static Mutex m_Mutex;

        /// <summary>
        /// 获取初始化值
        /// </summary>
        /// <returns>如果已经初始化，那就返回true;如果没初始化，那就返回false</returns>
        public static bool GetHasInit()
        {
            return m_HasInit;
        }

        public static void BaseInit()
        {
            m_HasInit = true;
            m_Mutex = new Mutex();
            m_OnePack = new byte[m_MaxOnePackBuff + 1];
            m_OnePackIndex = 0;
        }

        public static void BaseRelease()
        {
            m_Mutex.Close();
        }

        protected static void Lock()
        {
            m_Mutex.WaitOne();
            Debug.Print("Lock:" + Thread.CurrentThread.ManagedThreadId.ToString());
        }

        protected static void UnLock()
        {
            m_Mutex.ReleaseMutex();
            Debug.Print("Unlock:" + Thread.CurrentThread.ManagedThreadId.ToString());
        }
    }
}

